import java.util.*;

/**
 * 成绩排序
 */
public class ScoreSorted {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int num = Integer.parseInt(scanner.nextLine());
            int order = Integer.parseInt(scanner.nextLine());
            HashMap<String, Integer> scoreMap = new HashMap<>();
            List<Student> studentList = new ArrayList<>();
            for (int i=0; i<num; i++) {
                String scoreLine = scanner.nextLine();
                studentList.add(new Student(scoreLine.split(" ")[0], Integer.parseInt(scoreLine.split(" ")[1])));
            }
            studentList.sort(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o2.score-o1.score;
                }
            });
            List<Map.Entry<String, Integer>> list = new ArrayList<>(scoreMap.entrySet());
            if (order==0) {
                studentList.sort(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.score-o1.score;
                    }
                });
            } else {
                //正序
                studentList.sort(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.score-o2.score;
                    }
                });
            }
            studentList.forEach(student -> System.out.println(student.name + " " + student.score));
        }
    }

    static class Student{
        private String name;
        private int score;
        private Student(String name, int score) {
            this.name = name;
            this.score = score;
        }
    }
}
